import React from 'react';
import moment from 'moment';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Row, Col, Input, Modal } from 'antd';
const { TextArea } = Input;
const confirm = Modal.confirm;
const FormItem = Form.Item;
import { connect } from 'dva';
import { ModalC, ProductGroupTreeC, SecuritiesCodeC, InputNumberC } from '../../../../comps';

@Form.create()
class unfreezeC extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      userName: '', // 登录人名称
    };
  }

  componentDidMount() {
    this.props.dispatch({
      type: 'positionManageM/getUser',
      payload: localStorage.getItem('antd-pro-superManId'),
      callback: (data) => {
        if (data) {
          this.setState({
            userName: data.data.name,
          });
        }
      },
    });
  }

  // 模态框内容
  addForm = () => {
    const { getFieldDecorator } = this.props.form;
    const { tableData } = this.props;
    const formItemLayout = {
      labelCol: {
        span: 6,
      },
      wrapperCol: {
        span: 18,
      },
    };
    return (
      <div>
        <Form>
          {/* ***************************第一行************************************* */}
          <Row>
            <Col span={12}>
              <ProductGroupTreeC
                form={this.props.form}
                dataIndex='fundId'
                title='产品组合'
                authority={'write'}
                initialValue={tableData ? tableData.combiId : undefined}
                formItemLayout={formItemLayout}
                disabled={true}
                multiple={false}
              />
            </Col>
            <Col span={12}>
              <SecuritiesCodeC
                form={this.props.form}
                dataIndex='jycode'
                title='交易代码'
                initialValue={tableData ? tableData.tradingcode : undefined}
                extraLayout={formItemLayout}
                disabled={true}
              />
            </Col>
          </Row>
          {/* ***************************第二行************************************* */}

          <Row>
            <Col span={12}>
              <InputNumberC
                title='取消数量'
                dataIndex='adjustAmount'
                placeholder='请输入'
                precision={0}
                min={1}
                max={tableData.amount}
                form={this.props.form}
                formItemLayout={formItemLayout}
                rules={[
                  { required: true, message: `取消解冻冻结数量` },
                  { pattern: /^[1-9]/, message: '数量不能小于1' },
                ]}
              />
            </Col>
          </Row>
        </Form>
      </div>
    );
  };

  // 确认
  addanOK = () => {
    const {
      form: { validateFields, resetFields },
      tableData,
      type,
    } = this.props;
    validateFields(['adjustAmount'], (err, fieldsValue) => {
      if (err) return;
      const value = {
        amount: fieldsValue.adjustAmount.toString(),
        dataDt: tableData.dataDt,
        miniUnitId: tableData.combiCode,
        operateBy: this.state.userName,
        operateType: '5_1',
        securityType: tableData.securityType,
        tradingcode: tableData.tradingcode,
        exchangeCode: tableData.exchangeCode,
      };
      this.props.dispatch({
        type: 'positionManageM/adjustSecurity',
        payload: value,
        callback: (res) => {
          if (res) {
            resetFields();
            this.props.changeVisible();
            this.props.onRefesh(); //刷新列表
          }
        },
      });
    });
  };

  // 取消
  addanCancel = () => {
    const {
      form: { resetFields },
    } = this.props;
    this.props.changeVisible();
    resetFields();
  };

  render() {
    return (
      <ModalC
        title={'证券取消冻结解冻'}
        visible={this.props.visible}
        maskClosable={false}
        onOk={this.addanOK}
        onCancel={this.addanCancel}
        confirmLoading={this.props.loading.effects['positionManageM/adjustSecurity'] === true}
        width={683}
      >
        <div>{this.addForm()}</div>
      </ModalC>
    );
  }
}
export default connect(({ positionManage, loading }) => ({ positionManage, loading }))(unfreezeC);
